import { defineStore } from 'pinia'
import type { PatrolDormitoryEvent } from '../types/PatrolDormitoryEvent'

/**
 * 巡寝室事件存储模块
 * 负责管理巡寝室事件的全局状态
 */
export const usePatrolDormitoryEventStore = defineStore('patrolDormitoryEvent', {
    state: () => ({
        // 所有巡寝室事件列表
        events: [] as PatrolDormitoryEvent[],
        // 当前选中的事件
        currentEvent: null as PatrolDormitoryEvent | null,
        // 加载状态
        loading: false,
        // 筛选条件
        filter: {
            building: null as string | null,
            floor: null as string | null
        }
    }),
    actions: {
        /**
         * 获取所有巡寝室事件
         */
        async fetchAllEvents() {
            this.loading = true
            try {
                // this.events = await eventApi.getAllPatrolDormitoryEvents()
            } finally {
                this.loading = false
            }
        },

        /**
         * 创建新的巡寝室事件
         * @param newEvent - 新巡寝室事件对象
         */
        async createEvent(newEvent: Omit<PatrolDormitoryEvent, 'patrolDormitoryEventId'>) {
            this.loading = true
            try {
                // const created = await eventApi.createPatrolDormitoryEvent(newEvent)
                // this.events.unshift(created)
            } finally {
                this.loading = false
            }
        },

        /**
         * 设置楼栋筛选条件
         * @param building - 楼栋名称
         */
        setBuildingFilter(building: string | null) {
            this.filter.building = building
        },

        /**
         * 设置楼层筛选条件
         * @param floor - 楼层
         */
        setFloorFilter(floor: string | null) {
            this.filter.floor = floor
        }
    },
    getters: {
        /**
         * 获取筛选后的事件列表
         */
        filteredEvents(state): PatrolDormitoryEvent[] {
            let result = [...state.events]

            if (state.filter.building) {
                result = result.filter(e =>
                    e.keyCheckArea?.includes(`楼栋:${state.filter.building}`) ||
                    e.keyCheckArea?.includes(state.filter.building!)
                )
            }

            if (state.filter.floor) {
                result = result.filter(e =>
                    e.keyCheckArea?.includes(`楼层:${state.filter.floor}`) ||
                    e.keyCheckArea?.includes(`${state.filter.floor}楼`)
                )
            }

            return result.sort((a, b) =>
                new Date(b.patrolTime).getTime() - new Date(a.patrolTime).getTime()
            )
        },

        /**
         * 获取所有涉及的楼栋列表
         */
        buildingList(state): string[] {
            const buildings = new Set<string>()
            state.events.forEach(event => {
                if (event.keyCheckArea) {
                    const match = event.keyCheckArea.match(/楼栋:([^,]+)/)
                    if (match) {
                        buildings.add(match[1])
                    }
                }
            })
            return Array.from(buildings)
        }
    }
})
